package com.ddyx.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import com.ddyx.entity.EventClickLike;
import com.ddyx.entity.EventLog;
import com.ddyx.mapper.EventClickLikeMapper;
import com.ddyx.mapper.EventLogMapper;
import com.ddyx.service.EventClickLikeService;
import com.ddyx.util.EventTag;
import com.ddyx.util.MD5FileUtil;
import com.ddyx.util.MyBeanUtils;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Slf4j
@Service
public class EventClickLikeServiceImpl implements EventClickLikeService {
	@Resource
	EventClickLikeMapper eventClickLikeMapper;
	@Resource
	EventLogMapper eventLogMapper;

	/*
	 * 点赞
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm insertEventClickLike(RequestForm param, HttpServletRequest req) {
		ResponseForm result = new ResponseForm();
		EventClickLike eventClickLike = new EventClickLike();
		String requestUrl = req.getRequestURL().toString();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			MyBeanUtils.populate(eventClickLike, paramMap);
			eventClickLike.setEventTime(new Date());
			eventClickLikeMapper.insertSelective(eventClickLike);

			{// 同步用户事件信息
				EventLog eventLog = new EventLog();
				eventLog.setWxUid(eventClickLike.getWxUid());
				eventLog.setEventTag(EventTag.CLICKLIKE);
				eventLog.setEventTime(eventClickLike.getEventTime());
				eventLog.setStoreNo(eventClickLike.getStoreNo());
				eventLog.setRequestUrl(requestUrl);
				eventLog.setUrlMd5(MD5FileUtil.getMD5String(requestUrl));
				eventLogMapper.insertSelective(eventLog);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}

		return result;
	}

	/*
	 * 取消点赞
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm cancelEventClickLike(RequestForm param, HttpServletRequest req) {
		ResponseForm result = new ResponseForm();
		EventClickLike eventClickLike = new EventClickLike();
		String requestUrl = req.getRequestURL().toString();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			MyBeanUtils.populate(eventClickLike, paramMap);
			Example example = new Example(EventClickLike.class);
			Criteria criteria = example.createCriteria();
			criteria.andEqualTo("wxUid", eventClickLike.getWxUid());
			criteria.andEqualTo("goodsId", eventClickLike.getGoodsId());
			eventClickLikeMapper.deleteByExample(example);
			// eventClickLikeMapper.delete(eventClickLike);
			
			
			{
				// 取消点赞用户事件同步信息
				EventLog eventLog = new EventLog();
				eventLog.setEventTag(EventTag.CANCELLIKE);
				eventLog.setEventTime(new Date());
				eventLog.setRequestUrl(requestUrl);
				eventLog.setStoreNo(eventClickLike.getStoreNo());
				eventLog.setUrlMd5(MD5FileUtil.getMD5String(requestUrl));
				eventLog.setWxUid(eventClickLike.getWxUid());
				eventLogMapper.insertSelective(eventLog);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}

		return result;
	}

	/*
	 * 分页查询用户点赞过的商品列表
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm findEventClickLikeByCustomer(RequestForm param) {
		ResponseForm result = new ResponseForm();
		EventClickLike eventClickLike = new EventClickLike();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
//			MyBeanUtils.populate(eventClickLike, paramMap);
			com.ddyx.util.Page.parse(paramMap);
			Page<EventClickLike> pageHelper = PageHelper.startPage((int) paramMap.get("page"),
					(int) paramMap.get("size"));
			List<EventClickLike> list = eventClickLikeMapper.findEventClickLikeByCustomer(paramMap);
			result.setData(list);
			result.setTotal((int) pageHelper.getTotal());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

	/*
	 * 门店查询点赞用户列表
	 */
	@Override
	public ResponseForm getLikeCustomer(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			paramMap = com.ddyx.util.Page.parsePage(paramMap);
			Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
			List<Map<String, Object>> likeCustomer = eventClickLikeMapper.getLikeCustomer(paramMap);
			result.setData(likeCustomer);
			result.setTotal((int) pageHelper.getTotal());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}

		return result;
	}

}
